

package com.bolt.support.desensitization.resolver;

import org.springframework.http.ResponseEntity;

import java.lang.reflect.AnnotatedParameterizedType;
import java.lang.reflect.AnnotatedType;

/**
 * 用来解析返回值类型为{@link ResponseEntity}的类型解析器
 *
 * @author zyc
 */
public class ResponseEntityTypeResolver implements TypeResolver<ResponseEntity<Object>, AnnotatedParameterizedType> {

    private final int order = TypeResolvers.randomOrder();

    @Override
    public ResponseEntity<Object> resolve(ResponseEntity<Object> responseEntity, AnnotatedParameterizedType annotatedParameterizedType) {
        AnnotatedType typeArgument = annotatedParameterizedType.getAnnotatedActualTypeArguments()[0];
        Object erased = TypeResolvers.resolve(responseEntity.getBody(), typeArgument);
        return new ResponseEntity<>(erased, responseEntity.getHeaders(), responseEntity.getStatusCode());
    }

    @Override
    public boolean support(Object value, AnnotatedType annotatedType) {
        return value instanceof ResponseEntity && annotatedType instanceof AnnotatedParameterizedType;
    }

    @Override
    public int order() {
        return order;
    }
}
